package com.ec.edu.common.hello.send.impl;

import com.ec.edu.common.constants.NettyMap;
import com.ec.edu.common.hello.send.MsgSendService;
import com.ec.edu.common.vo.MessageVo;
import io.netty.channel.ChannelId;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

@Slf4j
public class NormalMsgSendService implements MsgSendService {
    @Override
    public void send(MessageVo messageVo) {
        ChannelId id = NettyMap.channelMap.get(messageVo.getClientId());
        SocketChannel socketChannel = (SocketChannel) NettyMap.channels.find(id);
        if (socketChannel != null && socketChannel.isOpen()) {
            if(StringUtils.isEmpty(messageVo.getMsgType())){
                log.error("消息类型不能为空");
            } else {
                socketChannel.writeAndFlush(messageVo);
            }
        }
        else{
            log.error("客户端未连接服务器,发送消息失败!");
        }
    }
}
